// app.js
App({
  onLaunch: function (res) {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力');
    } else {
      wx.cloud.init({
        // env 参数说明：
        //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境（第一个创建的环境）
        env: 'manbu-2ggvwdnk1d8bd0fa',
        traceUser: true,
      });
    }
    var openid = "";
    let that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({ //  调用云函数获取openid
        name: 'getopenid',
        complete: res => {
            openid = res.result.userInfo.openId;
            that.globalData.openid = openid;
            console.log(openid);
            db.collection("infoDB").where({
                _openid: openid
            }).get({
                success: function(res){
                    if ( res.data.length == 0 ) {
                        let tempnNickname = "用户" + openid.substr(openid.length-4,openid.length); 
                        db.collection("infoDB").add({
                            data: {
                                avatar: "",
                                nickname: tempnNickname,
                                signature: "无",
                                favourite: {}
                            }
                        });
                    } else {
                        db.collection("messageDB").where({
                            targetID: app.globalData.openid
                        }).get({
                            success: function(res){
                                let message = res.data.length;
                                that.globalData.message = message;
                            }
                        });
                    }
                }
            })
        }
    })
    
    if (res.scene == 1044) {
        console.log(res.shareTicket)
        if (res.shareTicket) {
            const shareTicket = res.shareTicket
            console.log(shareTicket)
            wx.getShareInfo({
                shareTicket: shareTicket, 
                success(res) {
                    console.log(res)
                }
            });
        }
    }
  },
  getSystemInfo: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.systemInfo = res;
      }
    });
  },
  //全局点击事件
    globalData: {
        pageHeight: wx.getSystemInfoSync()['windowHeight'],
        systemInfo: null,//客户端设备信息
        userInfo: null,
        date: new Date()
    }
});
